package com.zhaojonhy.zhaopangwb.adapter;

import android.app.Activity;
import android.graphics.Bitmap;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.zhaojonhy.zhaopangwb.R;
import com.zhaojonhy.zhaopangwb.bean.Comments;
import com.zhaojonhy.zhaopangwb.bean.User;
import com.zhaojonhy.zhaopangwb.utils.DateUtils;
import com.zhaojonhy.zhaopangwb.utils.ImageOptHelper;

import java.util.List;

/**
 * Created by Administrator on 2016/10/31.
 */

public class CommentsTomeAdapter extends BaseAdapter {

    final static String TAG = StatusAdapter.class.getSimpleName() ;

    private Activity activity ;
    private List<Comments> commentList ;
    private ImageLoader imageLoader ;

    public CommentsTomeAdapter( Activity activity , List<Comments> commentList ) {
        this.activity = activity ;
        this.commentList = commentList ;
        Log.d(TAG,"commentList'size:" + commentList );
        imageLoader = ImageLoader.getInstance() ;
    }

    @Override
    public int getCount() {
        return commentList.size();
    }

    @Override
    public Comments getItem(int position) {
        return commentList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Holder holder = null ;
        if( convertView == null) {
            holder = new Holder();
            convertView = View.inflate(activity, R.layout.item_comments_tome, null);
            holder.ll_avatar = (LinearLayout)convertView.findViewById(R.id.ll_avatar) ;
            holder.iv_avatar = (ImageView)holder.ll_avatar.findViewById(R.id.iv_avatar) ;
            holder.tv_subhead = (TextView)holder.ll_avatar.findViewById(R.id.tv_subhead) ;
            holder.tv_caption = (TextView)holder.ll_avatar.findViewById(R.id.tv_caption) ;

            holder.tv_content = (TextView)convertView.findViewById(R.id.tv_content) ;

            holder.ret_iv_avatar = (ImageView)convertView.findViewById(R.id.ret_iv_avatar) ;
            holder.ret_tv_subhead = (TextView)convertView.findViewById(R.id.ret_tv_subhead) ;
            holder.ret_tv_content = (TextView)convertView.findViewById(R.id.ret_tv_content) ;
            convertView.setTag(holder);
        }else{
            holder = (Holder)convertView.getTag() ;
        }
        final Comments comments =getItem(position) ;
        final User user = comments.getUser() ;

        imageLoader.displayImage(user.getAvatar_large()
                ,holder.iv_avatar , ImageOptHelper.getAvatarOptions());
        holder.tv_subhead.setText(user.getName());
        //设置发微博时间和来源
        if( comments.getSource().equals("")){
            holder.tv_caption.setText(DateUtils.getShortTime(user.getCreated_at()));
        }else{
            holder.tv_caption.setText(DateUtils.getShortTime(user.getCreated_at())
                    +" 来自 "+ Html.fromHtml(comments.getSource()));
        }

        holder.tv_content.setText(comments.getText());

        imageLoader.displayImage(comments.getStatus().getUser().getAvatar_large(),
                holder.ret_iv_avatar );
        holder.ret_tv_subhead.setText("@"+comments.getStatus().getUser().getName());
        holder.ret_tv_content.setText(comments.getStatus().getText());

        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        return convertView;
    }

    class Holder {

        private LinearLayout ll_avatar ;
        private ImageView iv_avatar ;
        private TextView tv_subhead ;
        private TextView tv_caption ;
        private TextView tv_content ;

        private ImageView ret_iv_avatar ;
        private TextView ret_tv_subhead ;
        private TextView ret_tv_content ;

    }

}
